<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="车位编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入车位编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="楼层" prop="floorId">-->
<!--        <el-select v-model="queryParams.floorId" placeholder="请选择楼层" @change="handleQuery">-->
<!--          <el-option-->
<!--            v-for="item in sysParkFloorList"-->
<!--            :key="item.id"-->
<!--            :label="item.name"-->
<!--            :value="item.id">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--&lt;!&ndash;        <el-input&ndash;&gt;-->
<!--&lt;!&ndash;          v-model="queryParams.floorId"&ndash;&gt;-->
<!--&lt;!&ndash;          placeholder="请输入楼层编号"&ndash;&gt;-->
<!--&lt;!&ndash;          clearable&ndash;&gt;-->
<!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
<!--&lt;!&ndash;        />&ndash;&gt;-->
<!--      </el-form-item>-->
      <el-form-item label="停车价格" prop="price">
        <el-input
          v-model="queryParams.price"
          placeholder="请输入停车价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="可用状态" prop="isLock">
        <el-select v-model="queryParams.isLock" placeholder="请选择可用状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:spot:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:spot:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:spot:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="handleExport"
          v-hasPermi="['system:spot:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-circle-check"
          size="mini"
          :disabled="multiple"
          @click="handleUsable"
          v-hasPermi="['system:spot:export']"
        >可用</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="handleDisabled"
          v-hasPermi="['system:spot:export']"
        >不可用</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="spotData">
      <div class="floorData">
        <el-tabs tab-position="left" @tab-click="getSpotByFloorId">
          <el-tab-pane :label="item.name" v-for="item of sysParkFloorList" :key="item.id" :id="item.id">
            <el-table v-loading="loading" :data="spotList" @selection-change="handleSelectionChange" height="600">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="车位编号" align="center" prop="id" />
<!--              <el-table-column label="楼层名称" align="center" prop="sysParkFloor.name" />-->
              <el-table-column label="停车位编号" align="center" prop="code" />
              <el-table-column label="停车价格" align="center" prop="price" />
              <el-table-column label="可用状态" align="center" prop="isLock">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isLock"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['system:spot:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['system:spot:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>

    </div>


    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="楼层" prop="floorId">
          <el-select v-model="form.floorId" placeholder="请选择楼层">
            <el-option
              v-for="item in sysParkFloorList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="停车价格" prop="price">
          <el-input type="number" v-model="form.price" placeholder="请输入停车价格" />
        </el-form-item>
        <el-form-item label="可用状态" prop="isLock">
          <el-select v-model="form.isLock" placeholder="请选择可用状态">
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSpot, getSpot, delSpot, addSpot, updateSpot, listFloor, handleDisabled, handleUsable} from '@/api/system/spot'

export default {
  name: "Spot",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysParkFloor: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车位表格数据
      spotList: [],
      // 楼层管理表格数据
      sysParkFloorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        floorId: null,
        code: null,
        price: null,
        isLock: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        floorId: [
          { required: true, message: "请选择楼层", trigger: "blur" }
        ],
        price: [
          { required: true, message: "停车价格不能为空", trigger: "blur" },
          { pattern: /^[1-9]\d{0,2}$/, message: '楼层车位数量必须为1到999之间的整数', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getFloor()
  },
  methods: {
    /** 查询车位列表 */
    getList() {
      this.loading = true;
      listSpot(this.queryParams).then(response => {
        this.spotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        floorId: null,
        code: null,
        price: null,
        isLock: null
      };
      // this.sysParkFloorList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    getSpotByFloorId(data){
      this.queryParams.floorId = data.$attrs.id;
      this.queryParams.id = null;
      this.queryParams.code = null;
      this.queryParams.price = null;
      this.loading = true;
      listSpot(this.queryParams).then(response => {
        this.spotList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSpot(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSpot(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSpot(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车位编号为"' + ids + '"的数据项？').then(function() {
        return delSpot(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 复选框选中数据 */
    handleSysParkFloorSelectionChange(selection) {
      this.checkedSysParkFloor = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/spot/export', {
        ...this.queryParams
      }, `spot_${new Date().getTime()}.xlsx`)
    },
    // 可用
    handleUsable(row){
      const ids = row.id || this.ids;
      handleUsable(ids).then(response => {
        this.getList();
        this.$modal.msgSuccess(response.msg);
      });
    },
    handleDisabled(row){
      const ids = row.id || this.ids;
      handleDisabled(ids).then(response => {
        this.getList();
        this.$modal.msgSuccess(response.msg);
      });
    },
    // 获取停车场楼层数据
    getFloor(){
      listFloor(this.queryParams).then(response => {
        this.sysParkFloorList = response.rows;
        if (this.sysParkFloorList && this.sysParkFloorList.length > 0){
          this.queryParams.floorId = this.sysParkFloorList[0].id
          this.getList();
        }
      });
    }
  }
};
</script>
<style scoped>
.el-select {
  width: 100%; /* 设置容器的宽度为 100% */
}
.spotData {
  //display: flex;
  //justify-content: space-between;
}
.floorData {
  //width: 200px;
  margin: 10px;
}
</style>
